package com.link.base.base.autoconfig.autoconfig.service;

import com.link.base.base.autoconfig.autoconfig.dao.mybatis.mapper.LnkInnerFieldMapper;
import com.link.base.base.autoconfig.autoconfig.model.LnkInnerField;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class LnkInnerFieldServiceImpl extends BasicServiceImpl<LnkInnerField> implements LnkInnerFieldService {
    @Resource
    private LnkInnerFieldMapper lnkInnerFieldMapper;

    @Override
    public BasicMapper<LnkInnerField> getBasicMapper() {
        return lnkInnerFieldMapper;
    }

    @Override
    public void beforUpdate(LnkInnerField entity) throws Exception {

    }

    @Override
    public void beforInsert(LnkInnerField entity) throws Exception {
    }

    @Override
    public void beforUpsert(LnkInnerField entity) throws Exception {
        String fieldName = entity.getName();//映射到java的javaBean字段名称
        if (StringUtils.isBlank(fieldName)) {
            throw new ServiceException("PUBLIC-041");
        }
        fieldName = fieldName.replaceAll("\\s", "");
        char[] chars = fieldName.toCharArray();
        chars[0] = toLowerCase(chars[0]);
        fieldName = String.valueOf(chars);
        String columnName = entity.getColumnname();//数据库列名
        entity.setName(fieldName);//去除空格
        if (!StringUtils.isNull(columnName)) {
            entity.setColumnname(columnName.replaceAll("\\s", ""));
        }
    }

    public static char toLowerCase(char chars) {
        if (65 <= chars && chars <= 90) {  //大写字母
            chars ^= 32;  //转换为小写
        }
        return chars;
    }

}
